
<!--Author: Nguyen Minh Nhat. Email: minhnhat2110@gmail.com-->
<?php
function Paging($total,$currPage,$pageSize,$rowPerPage,$link)
{
	$pageNumber=1;
	$i=1;
	$totalPage=ceil($total/$rowPerPage);
	$start=0;
	$text='';
	if($currPage<=$totalPage)
	{
		if($currPage==1)
		{
			//$text.='Trang ';
			$pageNumber=$pageSize;
			if($pageNumber>$totalPage)
			{
				$pageNumber=$totalPage;
			}
			$start=1;
		}
		else
		{
			$text.='<li><a href="'.$link.'page=1">Trang đầu</a></li> ';
			$prevPage=$currPage-1;
			$text.='<li><a href="'.$link.'page='.$prevPage.'">Trang trước</a></li> ';
			//xử lý trường hợp ($TotalPage-$currPage)<$PageSize/2
			if(($totalPage-$currPage)<($pageSize/2))
			{
				$start=round($totalPage-$pageSize)+1;//WWWW
				if($start<=0)
					$start=1;
				$pageNumber=$totalPage;
			}
			else
			{
				if(($currPage-($pageSize/2))==0)
				{
					$start=1;
					$pageNumber=$currPage+($pageSize/2)+1;
					if($totalPage<$pageNumber)
						$pageNumber=$totalPage;
				}
				else
				{
					$start=round($currPage-($pageSize/2));//WWWW
					if($start<=0)
						$start=1;
					$pageNumber=$currPage+($pageSize/2);
					if($totalPage<$pageNumber)
						$pageNumber=$totalPage;
					else
						if($pageNumber<$pageSize)
							$pageNumber=$pageSize;
				}
			}
		}
		$i=$start;
		while($i<=$pageNumber)
		{
			if($i==$currPage)
				$text.='<li class="active"><a >'.$i.'</a></li> ';
			else
			{
				$text.='<li><a href="'.$link.'page='.$i.'">'.$i.'</a></li> ';
				
			}
			$i++;
		}
		if($currPage<$totalPage)
		{
			$nextPage=$currPage+1;
			$text.='<li><a href="'.$link.'page='.$nextPage.'">Trang sau</a></li> ';
			$text.='<li><a href="'.$link.'page='.$totalPage.'">Trang cuối</a></li>';
			
		}
		echo $text;
	}
}

?>
